package com.cloud.rou.common.executor;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
import org.checkerframework.checker.nullness.qual.Nullable;

/**
 * <p></p>
 *
 * @author GaoShuangWen
 * @since 2019-11-01 11:28
 */
public class DefaultListenableFuture<T> implements ExecutorFuture<T> {

    private ListenableFuture<T> listenableFuture;


    public DefaultListenableFuture(ListenableFuture<T> listenableFuture) {

        this.listenableFuture = listenableFuture;
    }

    @Override
    public void addListener(@Nullable GenericListenableFutureListener<T> listener) {
        Futures.addCallback(listenableFuture, new FutureCallback<T>() {
            @Override
            public void onSuccess(@Nullable T result) {
                listener.operationSuccess(result);
            }

            @Override
            public void onFailure(Throwable t) {
                listener.operationFailure(t);
            }
        }, MoreExecutors.directExecutor());
    }



}
